﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace pttk_htt_hr.GUI
{
    class HeaderCheckbox
    {
        int TotalCheckBoxes = 0;
        int TotalCheckedCheckBoxes = 0;
        CheckBox HeaderCheckBox = null;
        bool IsHeaderCheckBoxClicked = false;

        DataGridView dataGridView;
        string CheckBoxID;

        public HeaderCheckbox(DataGridView _DataGridView, string _CheckBoxID)
        { 
            this.dataGridView = _DataGridView;
            this.CheckBoxID = _CheckBoxID;

            DataGridViewEventAdder();

            TotalCheckBoxes = _DataGridView.RowCount;
            TotalCheckedCheckBoxes = 0;
        }

        public DataGridView createInstance()
        {
            return this.dataGridView;
        }
        // my method

        private void DataGridViewEventAdder()
        {
            AddHeaderCheckBox();

            //HeaderCheckBox.KeyUp += new KeyEventHandler(HeaderCheckBox_KeyUp);

            HeaderCheckBox.MouseClick += new MouseEventHandler(HeaderCheckBox_MouseClick);

            dataGridView.CellValueChanged += new DataGridViewCellEventHandler(dgvSelectAll_CellValueChanged);

            dataGridView.CurrentCellDirtyStateChanged += new EventHandler(dgvSelectAll_CurrentCellDirtyStateChanged);

            dataGridView.CellPainting += new DataGridViewCellPaintingEventHandler(dgvSelectAll_CellPainting);

            // BindGridView();
        }




        private void AddHeaderCheckBox()
        {
            HeaderCheckBox = new CheckBox();
            // Set size
            HeaderCheckBox.Size = new Size(16, 16);

            //Add the CheckBox into the DataGridView
            this.dataGridView.Controls.Add(HeaderCheckBox);
        }


        private void dgvSelectAll_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            /*
             * If want check box at first:  e.ColumnIndex = -1
             * 
             */
            if (e.RowIndex == -1 && e.ColumnIndex == this.dataGridView.Columns.Count - 1)
                ResetHeaderCheckBoxLocation(e.ColumnIndex, e.RowIndex);
        }
        private void ResetHeaderCheckBoxLocation(int ColumnIndex, int RowIndex)
        {
            //Get the column header cell bounds
            Rectangle oRectangle = this.dataGridView.GetCellDisplayRectangle(ColumnIndex, RowIndex, false);
          

            Point oPoint = new Point();

            oPoint.X = oRectangle.Location.X + (oRectangle.Width - HeaderCheckBox.Width) / 2 + 1;
            oPoint.Y = oRectangle.Location.Y + (oRectangle.Height - HeaderCheckBox.Height) / 2 + 1;

            //Change the location of the CheckBox to make it stay on the header
            HeaderCheckBox.Location = oPoint;

            
        }
        
       



        private void HeaderCheckBox_MouseClick(object sender, MouseEventArgs e)
        {
            HeaderCheckBoxClick((CheckBox)sender);
        }

        private void HeaderCheckBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Space)
                HeaderCheckBoxClick((CheckBox)sender);
        }
        

        private void HeaderCheckBoxClick(CheckBox HCheckBox)
        {
            IsHeaderCheckBoxClicked = true;

            foreach (DataGridViewRow Row in dataGridView.Rows)
                ((DataGridViewCheckBoxCell)Row.Cells[this.CheckBoxID]).Value = HCheckBox.Checked;

            dataGridView.RefreshEdit();

            TotalCheckedCheckBoxes = HCheckBox.Checked ? TotalCheckBoxes : 0;

            IsHeaderCheckBoxClicked = false;
        }

        private void RowCheckBoxClick(DataGridViewCheckBoxCell RCheckBox)
        {
            if (RCheckBox != null)
            {
                TotalCheckBoxes = dataGridView.RowCount;
                //Modifiy Counter;            
                if ((bool)RCheckBox.Value && TotalCheckedCheckBoxes < TotalCheckBoxes)
                    TotalCheckedCheckBoxes++;
                else if (TotalCheckedCheckBoxes > 0)
                    TotalCheckedCheckBoxes--;

                //Change state of the header CheckBox.
                if (TotalCheckedCheckBoxes < TotalCheckBoxes)
                    HeaderCheckBox.Checked = false;
                else if (TotalCheckedCheckBoxes == TotalCheckBoxes)
                    HeaderCheckBox.Checked = true;
            }
        }
        

        private void dgvSelectAll_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (!IsHeaderCheckBoxClicked)
                RowCheckBoxClick((DataGridViewCheckBoxCell)dataGridView[e.ColumnIndex, e.RowIndex]);
        }

        private void dgvSelectAll_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (dataGridView.CurrentCell is DataGridViewCheckBoxCell)
                dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
        }
    }
}
